package com.brillilab.common.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageInfo;

import cn.hutool.core.collection.CollUtil;


/**
 * 分页查询结果集封装对象
 * 
 * @author LJ
 */
public class PageVo<T> implements Serializable {
	private static final long serialVersionUID = 1L;

	public final static int PAGE_NUM = 1;

	public final static int PAGE_SIZE = 10;

	/**当前页码：默认1*/
	private int pageNum = PAGE_NUM;

	/**每页记录条数：默认10*/
	private int pageSize = PAGE_SIZE;

	/**分页数据*/
	private List<T> content;

	/**总记录数*/
	private int totalRow;

	/**起始记录数*/
	private int startRow;

	/**总页数*/
	private int totalPage;

	/**是否有下一页*/
	private boolean hasNext; 

	/**是否有上一页*/
	private boolean  hasPrevious; 

	/**
	 * 构造方法
	 */
	public PageVo() {
		this(PAGE_NUM, PAGE_SIZE, 0, new ArrayList<T>());
	}

	/**
	 * 构造方法
	 * @param pageNum 当前页码
	 * @param pageSize 每页记录条数
	 * @param total 总记录数
	 * @param content 分页数据
	 */
	public PageVo(int pageNum,int pageSize) {
		this.pageNum = pageNum;
		this.pageSize = pageSize;
		this.startRow = (this.pageNum-1)*this.pageSize;
	}
	
	/**
	 * 构造方法
	 * @param page
	 */
	public PageVo(IPage<T> page){
		this.pageNum = (int)page.getCurrent();
		this.pageSize = (int)page.getSize();
		this.startRow = (this.pageNum-1)*this.pageSize;
		this.totalRow = (int) page.getTotal();
		this.totalPage = getPages();
		this.hasNext = pageNum < totalPage;
		this.hasPrevious = pageNum > 1;
		this.content = page.getRecords();
	}
	
	/**
	 * 构造方法
	 * @param pageNum 当前页码
	 * @param pageSize 每页记录条数
	 * @param total 总记录数
	 * @param content 分页数据
	 */
	public PageVo(int pageNum,int pageSize,int totalRow,List<T> content) {
		this.pageNum = pageNum;
		this.pageSize = pageSize;
		this.startRow = (this.pageNum-1)*this.pageSize;
		this.totalRow = totalRow;
		this.totalPage = getPages();
		this.hasNext = pageNum < totalPage;
		this.hasPrevious = pageNum > 1;
		this.content = content;
	}
	
	
	/**
	 * 构造方法
	 * 只试用PageHelper插件查出来的List数据分页
	 * @param content 分页数据
	 */
	public PageVo(List<T> content) {
		PageInfo<T> pageInfo = new PageInfo<T>(content);
		this.pageNum = pageInfo.getPageNum();
		this.pageSize = pageInfo.getPageSize();
		this.startRow = (this.pageNum-1)*this.pageSize;
		this.totalRow = (int)pageInfo.getTotal();
		this.totalPage = getPages();
		this.hasNext = pageNum < totalPage;
		this.hasPrevious = pageNum > 1;
		this.content = content;
	}


	public int getPageSize() {
		return pageSize;
	}
	public int getPageNum() {
		return pageNum;
	}
	public List<T> getContent() {
		return CollUtil.isNotEmpty(content) ? content : CollUtil.toList();
	}
	

	public boolean isHasNext() {
		return hasNext;
	}

	public boolean isHasPrevious() {
		return hasPrevious;
	}

	public int getTotalRow() {
		return totalRow;
	}

	public void setTotalRow(int totalRow) {
		this.totalRow = totalRow;
	}

	public int getStartRow() {
		return startRow;
	}

	public void setStartRow(Integer startRow) {
		this.startRow = startRow;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setContent(List<T> content) {
		this.content = content;
	}

	public void setStartRow(int startRow) {
		this.startRow = startRow;
	}

	public void setHasNext(boolean hasNext) {
		this.hasNext = hasNext;
	}

	public void setHasPrevious(boolean hasPrevious) {
		this.hasPrevious = hasPrevious;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	
	public int getTotalPage() {
		return totalPage;
	}

	public int getPages() {
		if (this.pageSize == 0 || this.totalRow==0) {
            return 0;
        }
		return totalRow / pageSize + ((totalRow % pageSize) > 0 ? 1 : 0);
	}
}
